﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Xml.Serialization;

namespace SmartParking.Base
{
    public class PasswordEx
    {
        //public static readonly DependencyProperty IsMonitoringProperty =
        //  DependencyProperty.RegisterAttached("IsMonitoring", typeof(bool), typeof(PasswordEx),
        //      new UIPropertyMetadata(false, OnIsMonitoringChanged));

        //public static readonly DependencyProperty PasswordLengthProperty =
        //  DependencyProperty.RegisterAttached("PasswordLength", typeof(int), typeof(PasswordEx), new UIPropertyMetadata(0));

        public static readonly DependencyProperty PasswordProperty =
        DependencyProperty.RegisterAttached("Password", typeof(string), typeof(PasswordEx),
            new FrameworkPropertyMetadata(string.Empty, OnPasswordChanged));

        public static string GetPassword(DependencyObject obj)
        {
            return (string)obj.GetValue(PasswordProperty);
        }

        public static void SetPassword(DependencyObject obj, string value)
        {
            obj.SetValue(PasswordProperty, value);
        }

        private static void OnPasswordChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is PasswordBox passwordBox)
            {
                passwordBox.PasswordChanged -= PasswordBox_PasswordChanged;
                passwordBox.Password = (string)e.NewValue;
                passwordBox.PasswordChanged += PasswordBox_PasswordChanged;
            }
        }

        private static void PasswordBox_PasswordChanged(object sender, RoutedEventArgs e)
        {
            if (sender is PasswordBox passwordBox)
            {
                SetPassword(passwordBox, passwordBox.Password);
            }
        }


        //public static bool GetIsMonitoring(DependencyObject obj)
        //{
        //    return (bool)obj.GetValue(IsMonitoringProperty);
        //}

        //public static void SetIsMonitoring(DependencyObject obj, bool value)
        //{
        //    obj.SetValue(IsMonitoringProperty, value);
        //}



        //public static int GetPasswordLength(DependencyObject obj)
        //{
        //    return (int)obj.GetValue(PasswordLengthProperty);
        //}

        //public static void SetPasswordLength(DependencyObject obj, int value)
        //{
        //    obj.SetValue(PasswordLengthProperty, value);
        //}


        //private static void OnIsMonitoringChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        //{
        //    var pb = d as PasswordBox;
        //    if (pb == null)
        //    {
        //        return;
        //    }
        //    if ((bool)e.NewValue)
        //    {
        //        pb.PasswordChanged += PasswordChanged;
        //    }
        //    else
        //    {
        //        pb.PasswordChanged -= PasswordChanged;
        //    }
        //}

        //static void PasswordChanged(object sender, RoutedEventArgs e)
        //{
        //    var pb = sender as PasswordBox;
        //    if (pb == null)
        //    {
        //        return;
        //    }
        //    SetPasswordLength(pb, pb.Password.Length);
        //}
    }
}
